﻿using System;
using System.Windows.Forms;
using 售后客户管理.ModelLibrary;

namespace 售后客户管理.Forms
{
    public partial class FmHetong : Form
    {
        public FmHetong()
        {
            InitializeComponent();
        }

        public FmHetong(Customer customer, Hetong hetong, System.Data.SqlClient.SqlConnection sqlConnection)
        {
            InitializeComponent();
            Customer = customer;
            Hetong = hetong;
            SqlConnection = sqlConnection;
        }
        private Customer Customer;
        private Hetong Hetong;
        private System.Data.SqlClient.SqlConnection SqlConnection = null;

        private void FmHetongs_Load(object sender, EventArgs e)
        {
            if (SqlConnection != null)
            {
                this.cbbsHetongtype.DataSource = SqlClassLibrary.SqlGetSet.GetDictList("HetongType", SqlConnection);
                this.cbbsShangwu.DataSource = SqlClassLibrary.SqlGetSet.GetDictList("Shangwu", SqlConnection);
            }
            this.btnSave.Paint += DrawingImage.RoundButton_Paint;
            this.btnCancel.Paint += DrawingImage.RoundButton_Paint;
            this.BackColor = Common.MyColor.NewColor_LightLavender;
            this.btnSave.BackColor = Common.MyColor.NewColor_Lavender;
            this.btnCancel.BackColor = Common.MyColor.NewColor_Lavender;
            this.fbcHetongbh.DoubleClick += FbcHetongbh_DoubleClick;
            ObjectToApp(Hetong, Customer);
        }
        private void FbcHetongbh_DoubleClick(object sender, System.EventArgs e)
        {
            this.fbcHetongbh.Text = SqlClassLibrary.SqlGetSet.GetBh("hetong", SqlConnection);
        }
        private void FmHetong_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.S && e.Control)
            {
                btnSave_Click(btnSave, new EventArgs()); ;
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.fbcHetongbh.Text == "")
            {
                return;
            }
            Hetong hetong = AppToObject();
            if (SqlClassLibrary.SqlGetSet.SetHetong(hetong, SqlConnection) > 0)
                MessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK);
            this.Close();
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.DialogResult = DialogResult.Cancel;
        }

        private void ObjectToApp(Hetong hetong, Customer customer)
        {
            this.fbcCustname.Text = customer.Custname;
            this.tbCustid.Text = customer.Custid;
            if (hetong == null)
            {
                this.cbBeactive.Checked = true;
                this.fbcHetongbh.Focus();
                return;
            }
            this.cbBeactive.Checked = hetong.Beactive == "Y" ? true : false;
            this.fbcBeizhu.Text = hetong.Beizhu;
            this.fbcEnddate.Text = hetong.Enddate;
            this.fbcHetje.Text = hetong.Hetje;
            this.fbcHetongbh.Text = hetong.Hetongbh;
            this.tbHetongid.Text = hetong.Hetongid;
            this.cbbsHetongtype.Text = hetong.Hetongtype;
            this.fbcJiaofeiDate.Text = hetong.JiaofeiDate;
            this.cbbsShangwu.Text = hetong.Shangwu;
            this.fbcStartdate.Text = hetong.Startdate;
        }
        private Hetong AppToObject()
        {
            Hetong hetong = new Hetong();
            hetong.Custid = this.tbCustid.Text;
            hetong.Beactive = this.cbBeactive.Checked ? "Y" : "N";
            hetong.Beizhu = this.fbcBeizhu.Text;
            hetong.Enddate = this.fbcEnddate.Text;
            hetong.Hetje = this.fbcHetje.Text;
            hetong.Hetongbh = this.fbcHetongbh.Text;
            hetong.Hetongid = this.tbHetongid.Text;
            hetong.Hetongtype = this.cbbsHetongtype.Text;
            hetong.JiaofeiDate = this.fbcJiaofeiDate.Text;
            hetong.Shangwu = this.cbbsShangwu.Text;
            hetong.Startdate = this.fbcStartdate.Text;

            return hetong;
        }

    }
}
